package com.supply.dl.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.supply.dl.R;
import com.supply.dl.app.ApiService;
import com.supply.dl.app.adapter.NormalAdapter;
import com.supply.dl.app.adapter.RecyclerViewDivider;
import com.supply.dl.app.adapter.ViewHolder;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.app.http.BaseObserver;
import com.supply.dl.app.http.HttpMethod;
import com.supply.dl.app.http.RxSchedulers;
import com.supply.dl.bean.FormulaInfo;
import com.supply.dl.bean.SelectDictionaryBean;
import com.supply.dl.utils.DialogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 配方选择
 * <p>
 * Author: tian <br>
 * Time: 2018/8/21 15:31
 */
public class FormulaSelectActivity extends BaseActivity {

    @BindView(R.id.search_rv)
    RecyclerView search_rv;


    /**
     * 输入框
     */
    @BindView(R.id.input_et)
    EditText input_et;

    /**
     * 当前筛选条件
     */
    private String filer = "";

    private ArrayList<FormulaInfo> datas;
    private NormalAdapter<FormulaInfo> adapter;


    /**
     * 被选中项
     */
    private FormulaInfo selectBean;

    /**
     * OMS单号
     */
    private String omsOrderNo;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_material_select;
    }

    @Override
    protected void initView() {
        setTitleShow("配方选择");

        showLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (selectBean == null) {
                    finish();
                    return;
                }

                DialogUtils.showDialog(FormulaSelectActivity.this, "提示", "确定选择吗?", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        onSureSelect();
                    }
                }).show();

            }
        });

        setRightInVisible();

        datas = new ArrayList<>();
        adapter = new NormalAdapter<FormulaInfo>(this, datas) {
            @Override
            protected void convert(ViewHolder holder, FormulaInfo data) {
                ImageView state_img = holder.getView(R.id.check_img);
                state_img.setImageResource(data.isCheched() ?
                        R.mipmap.check_selected : R.mipmap.check_normal);

                holder.setText(R.id.material_code_tv, data.getSkuCode());
                holder.setText(R.id.material_name_tv, data.getSkuName());
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.item_material_select;
            }
        };

        adapter.setOnItemClickListener(new NormalAdapter.OnItemClickListener<FormulaInfo>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, FormulaInfo data, int position) {

                for (FormulaInfo depotBean : datas) {
                    if (depotBean.getId().equals(data.getId())) {
                        depotBean.setCheched(!data.isCheched());
                    }
                    //
                    else {
                        depotBean.setCheched(false);
                    }

                    if (depotBean.isCheched()) {
                        selectBean = depotBean;
                    }

                }

                adapter.notifyDataSetChanged();

            }
        });

        search_rv.addItemDecoration(new RecyclerViewDivider(this, LinearLayoutManager.VERTICAL));
        search_rv.setLayoutManager(new LinearLayoutManager(this));
        search_rv.setAdapter(adapter);


        omsOrderNo = getIntent().getStringExtra("omsOrderNo");

        getFormulaList();
    }


    /**
     * 配方查询
     */
    private void getFormulaList() {
        //开始进行网络的访问
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getFormuaList(omsOrderNo)
                .compose(RxSchedulers.<BaseInfo<List<FormulaInfo>>>compose(this))
                .subscribe(new BaseObserver<List<FormulaInfo>>() {
                    @Override
                    public void onSuccess(BaseInfo<List<FormulaInfo>> baseInfo) {
                        if (baseInfo != null) {
                            if (baseInfo.getData() != null) {
                                datas.clear();
                                datas.addAll(baseInfo.getData());
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        Toast.makeText(FormulaSelectActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }
                });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && selectBean != null) {

            DialogUtils.showDialog(this, "提示", "确定选择吗?", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    onSureSelect();
                }
            }).show();

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    /**
     * 确定选择
     */
    private void onSureSelect() {
        Intent intent = new Intent();
        intent.putExtra("selectBean", selectBean);
        this.setResult(RESULT_OK, intent);
        finish();
    }

}










